package org.pupil.socket.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.pupil.common.RpcFuture;
import org.pupil.common.RpcRequestHolder;
import org.pupil.socket.codec.RpcProtocol;
import org.pupil.socket.codec.RpcResponse;

/**
 * @Description: 服务发送方响应消息处理器
 * @Author: pupil
 * @Date: 2024/07/09 下午 4:16
 */
public class ClientHandler extends SimpleChannelInboundHandler<RpcProtocol<RpcResponse>> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcProtocol<RpcResponse> rpcProtocol) throws Exception {
        // 获取请求id
        long requestId = rpcProtocol.getMsgHeader().getRequestId();
        // 获取异步监听器
        RpcFuture<RpcResponse> future = RpcRequestHolder.REQUEST_MAP.get(requestId);
        // 获取处理异步响应消息的监听器，设置异步返回成功并设置返回消息
        future.getPromise().setSuccess(rpcProtocol.getBody());
    }
}
